package com.data.action;

import com.client.bean.SaFeAPK;
import com.data.dbdao.IndexDao;
import com.data.util.CheckDataUtils;
import com.data.util.FileResumeUtils;
import com.data.util.FinalStringUtils;
import com.data.util.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @createTime: 2018/2/28
 * @author: HingLo
 * @description: 安全鱼首页
 */
@Controller
public class Index {
    @Autowired
    private IndexDao indexDao;

    @RequestMapping("/")
    public String index(HttpServletRequest request) {
        SaFeAPK saFeAPK = indexDao.listDao();
        request.setAttribute("dataMap", saFeAPK);
        return "index";
    }

    /**
     * @param version
     * @return
     */
    @RequestMapping("checkSafe")
    @ResponseBody
    public Map<String, Object> updateSafe(@RequestParam("version") String version) {

        SaFeAPK saFeAPK = indexDao.listDao();
        String old = saFeAPK.getVersion();
        if (version.equals(old)) {
            return JsonUtils.returnMassageUtils(false, JsonUtils.returnMassageUtils(false, "暂时更新"));
        } else {
            return JsonUtils.returnMassageUtils(true, JsonUtils.returnMassageUtils(true, saFeAPK));
        }
    }

    @RequestMapping("download")
    public void download(@RequestParam("md5") String md5, HttpServletRequest request, HttpServletResponse response) {
        if (CheckDataUtils.stringUtils(md5)) {
            md5 = md5.trim();
            String fileName = md5 + ".apk";
            //软件新的路径
            String filePath = FinalStringUtils.APKPATH + fileName;
            try {
                FileResumeUtils.downloadApk(fileName, filePath, request, response);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}
